//
//  ZNewMeetingVC.m
//  WeClubs
//
//  Created by Zed Link on 21/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZNewMeetingVC.h"

@interface ZNewMeetingVC (){

    BOOL _isPublish;//记录是否是会议发布
    BOOL _isOn;//关闭 or 打开
    
}
@property(nonatomic,copy)NSString *content;

@end

@implementation ZNewMeetingVC

-(instancetype)initWithisPublish:(BOOL)isPublish{

    if (self = [super init]) {
        _isPublish = isPublish;
    }
    return self;
}
#pragma mark - About Navigation

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"会议发布";
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"发布" andSelector:@selector(newMeetingAction)];
}

- (void)newMeetingAction
{
    
    [MBProgressHUD zAlertProgress:@"正在加载中"];
    
  NSDictionary *params = @{
                        @"content" :_content, //会议简要内容
                        @"club_id" :@"1", //会议发送对象的社团id
                        @"address" : @"", // 	会议举办地址
                        @"deadline":@"",//会议开始时间（即截止时间）
                        @"need_sign":@"", //是否需要签到，0：不需要签到，1：二维码签到
                        @"participation":@"", //会议参与人员id，使用,连接，例如1,4,6,10
                        @"leader":@"",//签到负责人id，当need_sign=1时，该参数必传。使用,连接，例如1,4
                             };
    [HTTPRequest POST:kPublic_meetingUrl parameter:params success:^(id resposeObject) {
       
        
    if ([resposeObject[@"result_code"] integerValue] == ZNetManagerStatusSuccess) {
         [MBProgressHUD zAlertHideHUD];
       
        
        
        
    }else{
        [MBProgressHUD zAlertMessage:resposeObject[@"result_msg"] to:self.view afterDelay:1.0];
    }
    } failure:^(NSError *error) {
     
        [MBProgressHUD zAlertHideHUD];
    }];
    
    // Todo: 发布会议
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *sectionConfig = self.interfaceConfig[indexPath.section];
    NSMutableArray *dataArr = sectionConfig[NewDataKey];
    NSDictionary *dataDict = dataArr[indexPath.row];
    
    switch ([dataDict[NewTypeKey] unsignedIntegerValue]) {
        case ZNewCellTypeTextField:
        {
            Z_WEAK_SELF;
            ZNewTextViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NewTextViewCellReuseID];
            [cell setPlaceHolderText:dataDict[NewTitleKey]];
            cell.textViewTextHandlerCallBack = ^(NSString * textF){
                weakSelf.content = textF;
                
            };
            return cell;
        }
            break;
        case ZNewCellTypeMemberChose:
        {
            ZNewMemberChoseCell *cell = [tableView dequeueReusableCellWithIdentifier:NewMemberChoseCellReuseID];
            [cell setTitle:dataDict[NewTitleKey]];
            return cell;
        }
            break;
        case ZNewCellTypeTextInput:
        {
            ZNewTextFieldCell *cell = [tableView dequeueReusableCellWithIdentifier:NewTextFieldCellReuseID];
            [cell setTitle:dataDict[NewTitleKey]];
            return cell;
        }
            break;
        case ZNewCellTypeSwitch:
        {
            ZNewSwitchCell *cell = [tableView dequeueReusableCellWithIdentifier:NewSwitchCellReuseID];
            [cell setTitle:dataDict[NewTitleKey]];
            
            Z_WEAK_SELF
            cell.didSwitchBlock = ^(BOOL isOn)
            {
                _isOn = isOn;
                NSString *headerTitle = @"签到负责人";
                if (isOn) {
                    // 添加签到负责人, 插入为第3组
                    NSDictionary *dataConfig = @{
                                                 NewTypeKey : @(1),
                                                 NewTitleKey : @"请选择负责签到的成员",
                                                 };
                    NSDictionary *sectionConfig = @{
                                                    NewHeaderKey : headerTitle,
                                                    NewDataKey : @[dataConfig],
                                                    };
                    [weakSelf.interfaceConfig insertObject:sectionConfig atIndex:2];
                } else
                {
                    // 删除签到负责人, 移除第3组
                    NSDictionary *sectionConfig = weakSelf.interfaceConfig[2];
                    if ([sectionConfig[NewHeaderKey] isEqualToString:headerTitle]) {
                        [weakSelf.interfaceConfig removeObjectAtIndex:2];
                    }
                }
                [weakSelf.mainView reloadData];
            };
            return cell;
        }
            break;
        default:
        {
            return nil;
        }
            break;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    NSMutableDictionary *sectionConfig = self.interfaceConfig[indexPath.section];
    NSMutableArray *dataArr = sectionConfig[NewDataKey];
    NSDictionary *dataDict = dataArr[indexPath.row];
    
    switch ([dataDict[NewTypeKey] unsignedIntegerValue]) {
        case ZNewCellTypeSwitch:
            
            if (_isOn) {

                
            }else{
            
            
            }
            
            break;
          default:
            break;
    
    }
    

}

@end
